package org.springside.examples.showcase.utilities.wb;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springside.examples.showcase.wb.entity.WbContent;
import org.springside.examples.showcase.wb.entity.WbContentRet;
import org.springside.modules.utils.DateUtils;
import org.springside.modules.utils.RegexUtil;

/**
 * 新浪微博抓取
 * 
 */
public class SinaWbParse {
	private static Logger log = Logger.getLogger(SinaWbParse.class);

	/**
	 * 分析网页内容提取属性
	 * 
	 * @param urlcontent
	 * @param fullfilename
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public static WbContent parseContent(String wbcontent) {
		wbcontent = wbcontent.replaceAll("\\\\n", "");
		wbcontent = wbcontent.replaceAll("\\\\t", "");
		// wbcontent = wbcontent.replaceAll("\n", "");
		String content = RegexUtil.getMatchGroup(wbcontent, "(<em nick-name[^>]*>(.*?)</em>)", 2);
		System.out.println("正文：" + content);

		String author = RegexUtil.getMatchGroup(wbcontent, "(CONFIG\\[\'onick\'\\]\\s*=\\s*\'([^\']*)\')", 2);
		System.out.println("博主：" + author);
		// $CONFIG['oid'] = '2717762443';
		String oid = RegexUtil.getMatchGroup(wbcontent, "(CONFIG\\[\'oid\'\\]\\s*=\\s*\'([^\']*)\')", 2);
		oid = StringUtils.isEmpty(oid) ? "0" : oid;
		System.out.println("uid：" + oid);
		String mid = RegexUtil.getMatchGroup(wbcontent, "(mid\\s*=\\s*\"(.*?)\")", 2);
		mid = StringUtils.isEmpty(mid) ? "0" : mid;
		System.out.println("mid：" + mid);

		// <a href=\"javascript:void(0);\" node-type=\"forward_counter\">\u8f6c\u53d1(285)<\/a>
		String rttCount = RegexUtil.getMatchGroup(wbcontent, "(转发\\((.*?)\\))", 2);
		rttCount = StringUtils.isEmpty(rttCount) ? "0" : rttCount;
		System.out.println("转发：" + rttCount);
		String commtCount = RegexUtil.getMatchGroup(wbcontent, "(评论\\((.*?)\\))", 2);
		commtCount = StringUtils.isEmpty(commtCount) ? "0" : commtCount;
		System.out.println("评论：" + commtCount);

		// <a target="_blank" href="http://m.weibo.com/web/cellphone.php#mobile" rel="nofollow">
		String via = RegexUtil.getMatchGroup(wbcontent, "(<a .*rel=\"nofollow\">([^<]*?)<)", 2);
		System.out.println("via:" + via);

		String create_time = RegexUtil.getMatchGroup(wbcontent, "(\\s*(.*)\\s*来自)", 2).trim();
		System.out.println("时间1：" + create_time);
		if (create_time != null) {
			if (create_time.contains("月")) {
				String[] timeArray = create_time.split(" ");
				if (timeArray.length >= 2) {
					create_time = timeArray[0].replace('月', '-').replace('日', ' ') + timeArray[1];
					create_time = Calendar.getInstance().get(Calendar.YEAR) + "-" + create_time + ":00";
				}
			} else if (create_time.contains("今天")) {
				String[] timeArray = create_time.split(" ");
				if (timeArray.length >= 2) {
					create_time = timeArray[0].replace("今天", "") + timeArray[1];
					create_time = String.format("%1$tY-%1$tm-%1$te", Calendar.getInstance()) + " " + create_time + ":00";
				}
			} else if (create_time.contains("分钟前")) {
				create_time = create_time.substring(0, create_time.indexOf("分钟前"));
				Calendar cal = Calendar.getInstance();
				cal.setTimeInMillis(System.currentTimeMillis());
				cal.add(Calendar.MINUTE, 0 - Integer.valueOf(create_time).intValue());
				create_time = String.format("%1$tY-%1$tm-%1$te %1$tH:%1$tM:%1$tS", cal);
			}
		}
		System.out.println("时间：" + create_time);

		WbContent wb = new WbContent();
		wb.setIr_uid(oid);
		wb.setIr_screen_name(author);
		wb.setIr_status_content(content);
		wb.setIr_groupname("热点转发");
		wb.setIr_via(via);
		wb.setIr_mid(Long.parseLong(mid));
		wb.setIr_rttcount(Integer.parseInt(rttCount));
		wb.setIr_commtcount(Integer.parseInt(commtCount));

		wb.setIr_urlname("http://weibo.com/" + oid + "/" + SinaMid2Url.mid2url(mid));
		wb.setIr_hkey(wb.getIr_mid() + "");
		wb.setIr_sid(wb.getIr_mid());

		try {
			wb.setIr_created_at(DateUtils.parse(create_time, "yyyy-MM-dd hh:mm:ss"));
			wb.setIr_created_date(DateUtils.format(wb.getIr_created_at(), "yyyy-MM-dd"));
		} catch (ParseException e) {
			// log.error("下载内容解析有误", e);
		}

		return wb;
	}

	public static List<WbContent> parseComments(String wbcontent, Calendar calendar) {
		List<WbContent> wbcomments = new ArrayList<WbContent>();

		wbcontent = wbcontent.replaceAll("\\\\n", "");
		wbcontent = wbcontent.replaceAll("\\\\t", "");
		List<String> matchGroupList = RegexUtil.getMatchGroupList(wbcontent, "(?s)(<dl class=\"comment_list W_linecolor clearfix\" mid=.+?>)(.+?)</dl>", 2);
		for (String feedString : matchGroupList) {
			WbContent wb = new WbContent();
			// action-data="cuid=2310686612&ouid=2625503282&cid=3488652649182575&mid=3488460335784709&content=空空哈儿&type=big&ispower=1"
			String infos = RegexUtil.getMatchGroup(feedString, "(?s)(action-data=\"(.+?)\" action-type=\"replycomment\")", 2);
			String[] _infos = infos.split("&");
			for (String info : _infos) {
				String[] _info = info.split("=");
				if (_info != null && _info.length == 2) {
					if (_info[0].equalsIgnoreCase("cuid")) {
						// wb.setIr_status_content(content);
						wb.setIr_retweeted_uid(_info[1]);
					} else if (_info[0].equalsIgnoreCase("ouid")) {
						wb.setIr_uid(_info[1]);
					} else if (_info[0].equalsIgnoreCase("cid")) {
						wb.setIr_mid(Long.parseLong(_info[1]));
					} else if (_info[0].equalsIgnoreCase("mid")) {
						wb.setIr_retweeted_mid(Long.parseLong(_info[1]));
					} else if (_info[0].equalsIgnoreCase("content")) {
						wb.setIr_screen_name(_info[1]);
					}
				}
			}

			String create_time = RegexUtil.getMatchGroup(feedString, "(?s)(<span class=\"W_textb\">(.+?)</span>)", 2).trim();
			create_time = create_time.replaceAll("\\(|\\)", "");
			if (create_time != null) {
				if (create_time.contains("月")) {
					String[] timeArray = create_time.split(" ");
					if (timeArray.length >= 2) {
						create_time = timeArray[0].replace('月', '-').replace('日', ' ') + timeArray[1];
						create_time = Calendar.getInstance().get(Calendar.YEAR) + "-" + create_time + ":00";
					}
				} else if (create_time.contains("今天")) {
					String[] timeArray = create_time.split(" ");
					if (timeArray.length >= 2) {
						create_time = timeArray[0].replace("今天", "") + timeArray[1];
						create_time = String.format("%1$tY-%1$tm-%1$te", Calendar.getInstance()) + " " + create_time + ":00";
					}
				} else if (create_time.contains("分钟前")) {
					create_time = create_time.substring(0, create_time.indexOf("分钟前"));
					calendar.add(Calendar.MINUTE, 0 - Integer.valueOf(create_time).intValue());
					create_time = String.format("%1$tY-%1$tm-%1$te %1$tH:%1$tM:%1$tS", calendar);
				} else if (create_time.contains("秒前")) {
					create_time = create_time.substring(0, create_time.indexOf("秒前"));
					calendar.add(Calendar.SECOND, 0 - Integer.valueOf(create_time).intValue());
					create_time = String.format("%1$tY-%1$tm-%1$te %1$tH:%1$tM:%1$tS", calendar);
				}
			}

			try {
				wb.setIr_created_at(DateUtils.parse(create_time, "yyyy-MM-dd hh:mm:ss"));
				wb.setIr_created_date(DateUtils.format(wb.getIr_created_at(), "yyyy-MM-dd"));
			} catch (ParseException e) {
				// log.error("下载内容解析有误", e);
			}
			// <a href="/2625503282" title="空空哈儿"><img src="http://tp3.sinaimg.cn/2625503282/50/0/1" usercard="id=2625503282" width="30" height="30" alt="" /></a>
			String img = RegexUtil.getMatchGroup(feedString, "(?s)(<img src=\"(.*)\"  usercard=.*?>)", 2).trim();
			wb.setIr_thumbnail_pic(img);
			String screenname = wb.getIr_screen_name() + "</a>";
			int beginContent = feedString.indexOf(screenname);
			int endContent = feedString.indexOf("<span class=\"W_textb\">");
			String content = "";
			if (beginContent > 0 && endContent > beginContent) {
				content = feedString.substring(beginContent + screenname.length(), endContent);
			}
			wb.setIr_status_content(content.trim());
			wb.setIr_urlname("http://weibo.com/" + wb.getIr_uid() + "/" + SinaMid2Url.mid2url("" + wb.getIr_mid()));
			wb.setIr_hkey(wb.getIr_mid() + "");
			wb.setIr_sid(wb.getIr_mid());
			wb.setIr_rttcount(0);
			wb.setIr_commtcount(0);
			wb.setIr_via("");
			wb.setIr_groupname("消息传播分析");
			wbcomments.add(wb);
		}

		// for (WbContent wb2 : wbcomments) {
		// System.out.println(wb2.getIr_uid() + ":" + wb2.getIr_mid() + ":" + wb2.getIr_screen_name() + ":" + wb2.getIr_status_content() + ":" + wb2.getIr_retweeted_uid() + ":" + wb2.getIr_retweeted_mid());
		// }

		return wbcomments;
	}

	public static List<String> parseCommPageUrls(String wbcontent) {
		wbcontent = wbcontent.replaceAll("\\\\n", "");
		wbcontent = wbcontent.replaceAll("\\\\t", "");
		wbcontent = RegexUtil.getMatchGroup(wbcontent, "(?s)(<div class=\"W_pages W_pages_comment\" action-type=\"commentFilter\" >(.*?)</div>)", 1);
		// <a href="javascript:void(0);" action-type="feed_list_page" action-data="id=3488460335784709&max_id=3488851098160956&page=2">
		List<String> pageUrls = RegexUtil.getMatchGroupList(wbcontent, "(?s)(action-data=\"(.*?)\".*?action-type=\"feed_list_page\")", 2);
		return pageUrls;
	}

	/**
	 * 抽象微博转发路线
	 * 
	 * @param wb
	 * @return
	 */
	public static String[] getSpreadRoad(WbContentRet wb) {
		String status_content = wb.getIr_status_content();
		status_content = status_content.replaceAll("(</\\w*>)", " ").replaceAll("(<[^>]*>)", " ").replaceAll("(<?a target[^>]*>)", " ").trim();
		List<String> matchGroupList = RegexUtil.getMatchGroupList(status_content.replaceAll("回复", "//"), "(([/]{0,2}\\s?@[^\\s<>:]*)[\\s:]*)", 2);
		String spreadUsers = "";
		String touser = "";
		for (int i = matchGroupList.size() - 1; i >= 0; i--) {
			String username = matchGroupList.get(i);
			if (username.trim().equals("")) {
				continue;
			}
			if (username.contains("//")) {
				spreadUsers += "-->" + username.replaceAll("(//\\s@)", "").trim();
				if (!touser.trim().equals("")) {
					spreadUsers += "(" + touser + ")";
					touser = "";
				}
			} else {
				touser += username.trim() + ",";
			}
		}
		spreadUsers += "-->" + wb.getIr_screen_name();
		if (!touser.trim().equals("")) {
			spreadUsers += "(" + touser + ")";
		}
		spreadUsers = spreadUsers.substring(3);
		log.debug(spreadUsers + "\t" + wb.getIr_mid() + "\t" + wb.getIr_uid() + "\t" + SinaMid2Url.mid2url("" + wb.getIr_mid()) + "\t" + status_content);
		System.out.println(spreadUsers);
		String[] su = spreadUsers.split("-->");
		return su;
	}

	public static void main(String[] args) {
		try {
			File file = new File("E:\\WorkSpace\\TRSCIS\\socialInfo\\examples\\showcase\\data\\2310686612\\3488460335784709.html");
			Long time = file.lastModified();
			Calendar cd = Calendar.getInstance();
			cd.setTimeInMillis(time);
			String readFileToString = FileUtils.readFileToString(file, "UTF-8");
			// parseContent(readFileToString);
			// parseComments(readFileToString, cd);
			parseCommPageUrls(readFileToString);

		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
